#include "pool.h"
#include "Extern_global_Variable.h"

template <typename T>
T TDefaultPoolItemManager<T>::CreateInstance(Pointer AParameter)
{
	return T.T::T();
};

template <typename T>
procedure TDefaultPoolItemManager<T>::ClearInstance(T AInstance,
	Pointer AParameter)
{
	AInstance.Clear();
};

template <typename T>
procedure TDefaultPoolItemManager<T>::DestroyInstance(T AInstance,
	Pointer AParameter)
{
	AInstance.T::~T();
};

template <typename TItem, typename TItemManager>
typename TPool<TItem,TItemManager>::TItem TPool<TItem,TItemManager>::GetItem()
{
	TItem Result;
	if (FAvailableItems.vCount > 0)
	{
		Result = *FAvailableItems.Last;
		FAvailableItems.vCount = FAvailableItems.vCount - 1;
	}
	else
	{
		Result = TItemManager::CreateInstance(FParameter);
	};
	return Result;
};

template <typename TItem, typename TItemManager>
procedure TPool<TItem,TItemManager>::FreeItem(TItem AItem)
{
	TItemManager::ClearInstance(AItem, FParameter);
	FAvailableItems.Add(AItem);
};

template <typename TItem, typename TItemManager>
TPool<TItem,TItemManager>::TPool(Pointer AParameter)
{
	FParameter = AParameter;
	FAvailableItems.Create();
};

template <typename TItem, typename TItemManager>
TPool<TItem,TItemManager>::TPool()
{
	//FParameter = AParameter;
	FAvailableItems.Create();
};

template <typename TItem, typename TItemManager>
TPool<TItem,TItemManager>::~TPool()
{
	TItem Item;

	std::list<TItem>::iterator vIt;
	for (vIt = FAvailableItems.vList->begin(); vIt != FAvailableItems.vList->end(); ++vIt)
	{
		Item = (TItem)(*vIt);
		TItemManager::DestroyInstance(Item, FParameter);
	}

	//inherited Destroy;
};